package theMarshall;

	import data.Cal;
	import data.User;
	import data.Task;
	import java.io.*;
	import java.util.ListIterator;
	import java.util.logging.Level;
	import java.util.logging.Logger;
	import javax.xml.bind.JAXBContext;
	import javax.xml.bind.JAXBException;

	/**
	 *
	 * @author rao
	 */
	public class TaskMarshall {

	    public static void main(String args[]) throws IOException {
	        try {

	            // assign path to the task-manager Xml, 
	            String path = "/home/hectorbernal/Dropbox/MDS2/taskMarshall/webData/task-manager-xml.xml";

	            // create an instance context class, to serialize/deserialize.
	            JAXBContext jaxbContext = JAXBContext.newInstance(Cal.class);

	            // Create a file input stream for the task Xml.
	            FileInputStream stream = new FileInputStream(path);

	            // deserialize task-manager xml into java objects.
	            Cal cal = (Cal) jaxbContext.createUnmarshaller().unmarshal(stream);


	            // Iterate through the collection of task object and print each task object in the form of Xml to console.
	            ListIterator<Task> listIteratorTasks = cal.tasks.listIterator();
	            
	            System.out.println("Printing tasks objects serailized into Xml");

	            // Iterate through the collection of task object and print each task object in the form of Xml to console.
	            ListIterator<User> listIteratorUsers = cal.users.listIterator();
	            
	            System.out.println("Printing tasks objects serailized into Xml");
	            
	            while (listIteratorTasks.hasNext()) {
	                PrintTaskObject(listIteratorTasks.next());
	            }
	            while (listIteratorUsers.hasNext()) {
	                PrintUserObject(listIteratorUsers.next());
	            }

	            // Serialize university object into xml.
	            
	            StringWriter writer = new StringWriter();

	            // We can use the same context object, as it knows how to 
	            //serialize or deserialize University class.
	            jaxbContext.createMarshaller().marshal(cal, writer);

	            
	            System.out.println("Printing serialized cal Xml before saving into file!");
	            
	            // Print the serialized Xml to Console.
	            System.out.println(writer.toString());
	            
	            
	            // Finally save the Xml back to the file.
	            SaveFile(writer.toString(), path);



	        } catch (JAXBException ex) {
	            Logger.getLogger(TaskMarshall.class.getName()).log(Level.SEVERE, null, ex);
	        }





	    }

	    private static void PrintTaskObject(Task task) {

	        try {


	            StringWriter writer = new StringWriter();

	            // create a context object for Student Class
	            JAXBContext jaxbContext = JAXBContext.newInstance(Task.class);

	            // Call marshal method to serialize student object into Xml
	            jaxbContext.createMarshaller().marshal(task, writer);

	            System.out.println(writer.toString());

	        } catch (JAXBException ex) {
	            Logger.getLogger(TaskMarshall.class.getName()).log(Level.SEVERE, null, ex);
	        }




	    }

	    private static void PrintUserObject(User user) {

	        try {


	            StringWriter writer = new StringWriter();

	            // create a context object for Student Class
	            JAXBContext jaxbContext = JAXBContext.newInstance(User.class);

	            // Call marshal method to serialize student object into Xml
	            jaxbContext.createMarshaller().marshal(user, writer);

	            System.out.println(writer.toString());

	        } catch (JAXBException ex) {
	            Logger.getLogger(TaskMarshall.class.getName()).log(Level.SEVERE, null, ex);
	        }




	    }
	    
	    private static void SaveFile(String xml, String path) throws IOException {


	        File file = new File(path);

	        // create a bufferedwriter to write Xml
	        BufferedWriter output = new BufferedWriter(new FileWriter(file));

	        output.write(xml);

	        output.close();



	    }


}
